package com.cby.android.sample.activities

import android.text.TextUtils
import android.widget.Button
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cby.android.library.base.BaseStateActivity
import com.cby.android.library.base.callback.EmptyCallback
import com.cby.android.library.base.callback.ErrorCallback
import com.cby.android.library.base.callback.LottieLoadingCallback
import com.cby.android.library.base.callback.TimeoutCallback
import com.cby.android.library.ext.observe
import com.cby.android.library.utils.KeyboardUtils
import com.cby.android.library.utils.StringMatherUtils
import com.cby.android.library.widget.edittext.MClearEditText
import com.cby.android.library.widget.snackbar.MySnackbar
import com.cby.android.sample.R
import com.cby.android.sample.adapter.RollCommonQueryAdapter
import com.cby.android.sample.bean.roll.RollCommonBean
import com.cby.android.sample.intent.RollMoreIntent
import com.cby.android.sample.model.RollMoreViewModel
import com.cby.android.sample.state.RollMoreState
import com.kingja.loadsir.core.LoadSir
import kotlinx.coroutines.launch

/**
 * @description: RollPhoneAddressActivity
 *
 * @author ChenBaoyang
 * @date 4/23/21 14:32
 */
class RollPhoneAddressActivity : BaseStateActivity<RollMoreViewModel, RollMoreState, RollMoreIntent>() {

    private val queryBtn: Button by lazy { findViewById(R.id.btn_query) }
    private val phoneEdit: MClearEditText by lazy { findViewById(R.id.edit_text_phone) }
    private val recyclerView: RecyclerView by lazy { findViewById(R.id.recyclerView) }

    private lateinit var queryAdapter: RollCommonQueryAdapter

    override fun bindLiveData() {
        observe(viewModel!!.state) {
            it.dataPhone?.let { it1 ->
                val datas = mutableListOf<RollCommonBean>()
                datas.add(RollCommonBean("手机号码:", it1.mobile))
                datas.add(RollCommonBean("归属地省份:", it1.province))
                datas.add(RollCommonBean("归属地描述:", it1.carrier))
                queryAdapter.setNewData(datas)
            }
        }
    }

    override fun reload() {

    }

    override fun getLayoutId() = R.layout.roll_activity_phone_address

    override fun onViewInit() {
        title = "手机号码归属地查询"

        queryAdapter = RollCommonQueryAdapter(R.layout.roll_item_common_query)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = queryAdapter
        recyclerView.itemAnimator = DefaultItemAnimator()

        queryBtn.setOnClickListener {
            KeyboardUtils.hideInputKeyboard(this)

            //获取手机号码
            val phoneNumber: String = phoneEdit.getText().toString()

            if (TextUtils.isEmpty(phoneNumber)) {
                MySnackbar.makeSnackBarRed(toolbar, "手机号码不能为空")
                return@setOnClickListener
            }

            if (!StringMatherUtils.isPhoneLegal(phoneNumber)) {
                MySnackbar.makeSnackBarRed(toolbar, "手机号码格式不正确")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                viewModel!!.intents.send(RollMoreIntent.LoadRollMobileIntent(phoneNumber))
            }

        }
    }

    override fun initLoadSir() {
        val loadSir = LoadSir.Builder()
            .addCallback(LottieLoadingCallback())
            .addCallback(EmptyCallback())
            .addCallback(ErrorCallback())
            .addCallback(TimeoutCallback())
            .setDefaultCallback(EmptyCallback::class.java)
            .build()
        loadService = loadSir.register(getTargetView()){
            reload()
        }
    }

    override fun getTargetView() = recyclerView
}